const Controller = require('../core/base_controller');

class AppController extends Controller {
  //测试
  async test() {
    const { app } = this
    let Repair = app.table('repair')
    let ret = Repair.delete({
      id: '999999-6d78-480d-af98-c7f8d8e7a899',
    })

    this.success(ret)
  }

  // 保存
  async save() {
    const param = this.param;
    const { table } = param;

    console.log(table)

    const { app } = this;

    const result = await app.table(table).save(param);
    this.success(result);
  }

  // 删除
  async remove() {
    const param = this.param;
    const { table } = param;

    const { app } = this;

    const result = await app.table(table).delete(param);
    this.success(result);
  }

  // 查询单个
  async findOne() {
    const param = this.param;
    const { table } = param;
    const { app } = this;
    const result = await app.table(table).findOne(param);
    this.success(result);
  }

  // 查询所有
  async findAll() {
    const param = this.param;
    const { table } = param;
    const { app } = this;
    const result = await app.table(table).findAll(param);
    this.success(result);
  }

  //保存多个实体
  async saveMany() {
    const { table } = this.param;

    let arr = this.ctx.request.body

    let results = []

    for (var i = 0; i < arr.length; i++) {
      const result = await app.table(table).save(arr[i]);
      results.push(result)
    }

    this.success(results);
  }

  //分页接口
  async page() {
    const param = this.param;
    const { table } = param;
    const { app } = this;
    const result = await app.table(table).page(param);
    this.success(result);
  }

}
module.exports = AppController;
